﻿Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Configuration
Imports System.Management

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        lblNetStatTitle.Text = "Network Status:"
        getIPAddress()
        getMacAddress()
        'My.Settings.Reload()

        'Restores Previous User Settings
        If My.Settings.SaveCredentials = True Then
            restoreApplicationSettings()
        End If

        'https://www.wlan.wwu.edu/login.pl?action=paint;source=140.160.123.123;r=RTxMTBQ4320


    End Sub


    Function restoreApplicationSettings()
        cbSaveCredentials.Checked = My.Settings.SaveCredentials
        tbPassword.Text = My.Settings.Password
        tbUsername.Text = My.Settings.Username
        cbSaveCredentials.Checked = True
    End Function 'restoreApplicationSettings

    Function getIPAddress()
        Dim strHostName As String, strIPAddress As String
        Dim n As String
        strHostName = System.Net.Dns.GetHostName()

        For i As Integer = 0 To 10
            strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(i).ToString
            'MessageBox.Show("i= " & i & " strIPAddress: " & strIPAddress)
            n = strIPAddress.IndexOf(".")
            If n > 0 And n < 5 Then
                lblStatus.Text &= "IPAddress: " & strIPAddress & vbCrLf
                Exit For
            End If
        Next
    End Function 'getIPAddress
    Function getMacAddress()
        Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As NetworkInterface
        Dim strPhysicalAddress As String
        ' Determines whether you have an active internet connection
        ' NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
        For Each adapter In nics
            Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
            If adapter.NetworkInterfaceType().ToString = "Wireless80211" Then
                tbStatus.Text &= "Description: " & adapter.Description & vbCrLf & vbCrLf
                tbStatus.Text &= "Physical Address: " & adapter.GetPhysicalAddress().ToString() & vbCrLf & vbCrLf
                strPhysicalAddress = adapter.GetPhysicalAddress.ToString

                Dim builder As System.Text.StringBuilder = New System.Text.StringBuilder(strPhysicalAddress, 18)
                For i As Integer = 0 To 13 Step 3
                    builder.Insert(i + 2, ":")
                    'MessageBox.Show(builder.ToString.IndexOf(":"))
                Next

                lblStatus.Text &= "wLan Mac: " & builder.ToString
            End If
        Next adapter

        'split() stringbuilder
        'http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getphysicaladdress.aspx
    End Function 'getMacAddress
    Function getNetworkStatus()
        If NetworkInformation.NetworkInterface.GetIsNetworkAvailable = True Then
            'This appears to do the same thing...
            'My.Computer.Network.IsAvailable = True Then
            lblNetStatus.ForeColor = Color.Blue
            lblNetStatus.Text = "Connected"
        Else
            lblNetStatus.ForeColor = Color.Red
            lblNetStatus.Text = "Disconnected"
        End If

    End Function
    Private Sub btSaveConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSaveConfig.Click

        If cbSaveCredentials.Checked = True Then
            My.Settings.Username = tbUsername.Text
            My.Settings.Password = tbPassword.Text
            My.Settings.SaveCredentials = True

        Else
            'My.Settings.Username = ""
            'My.Settings.Password = ""
            My.Settings.SaveCredentials = False

        End If

        'My.Settings.Save()
        'it appears that I don't even need my.settings.save() to make this work... 
        'but there must be an easier way than setting these things manually
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click


        If tbUsername.Text = "" Or tbPassword.Text = "" Then
            MessageBox.Show("You must enter a valid username and password!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            wwuWirelessLogin()



            btnLogin.Enabled = False
            btnLogoff.Enabled = True
        End If

    End Sub
    Function wwuWirelessLogin()
        Dim myReq As HttpWebRequest = WebRequest.Create("https://www.wlan.wwu.edu")

        Dim response As HttpWebResponse = CType(myReq.GetResponse(), HttpWebResponse)
        tbStatus.Text = WebExceptionStatus.Success & vbCrLf & response.StatusDescription
        myReq.Method = "Post"

        'Dim success As Boolean
        'Dim resp As New Net.HttpWebResponse()
        'If success <> True Then
        '    MessageBox.Show("success false")
        'End If
    End Function 'wwuWirelessLogin
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogoff.Click
        btnLogin.Enabled = True
        btnLogoff.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        getNetworkStatus()
        'ListBox1.Items.Add(ListBox1.Items.Count)
    End Sub
    'Shared Sub ToggleConfigEncryption(ByVal exeConfigName As String)
    '    ' Takes the executable file name without the
    '    ' .config extension.
    '    Try
    '        ' Open the configuration file and retrieve 
    '        ' the connectionStrings section.
    '        Dim config As Configuration = ConfigurationManager. _
    '            OpenExeConfiguration(exeConfigName)

    '        Dim section As ConnectionStringsSection = DirectCast( _
    '            config.GetSection("connectionStrings"),  _
    '            ConnectionStringsSection)

    '        If section.SectionInformation.IsProtected Then
    '            ' Remove encryption.
    '            section.SectionInformation.UnprotectSection()
    '        Else
    '            ' Encrypt the section.
    '            section.SectionInformation.ProtectSection( _
    '              "DataProtectionConfigurationProvider")
    '        End If

    '        ' Save the current configuration.
    '        config.Save()

    '        Console.WriteLine("Protected={0}", _
    '        section.SectionInformation.IsProtected)

    '    Catch ex As Exception
    '        Console.WriteLine(ex.Message)
    '    End Try
    'End Sub

    Private Sub tbPassword_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbPassword.MouseHover
        tbPassword.PasswordChar = ""
    End Sub
    Private Sub tbPassword_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbPassword.MouseLeave
        tbPassword.PasswordChar = "*"
    End Sub
    'Public Sub SSID()
    '    System.Net.NetworkInformation.

    '    Try()
    '        Dim query As String = "SELECT * FROM MSNDis_80211_BSSIList WHERE Active= 'True'"

    '        Dim searcher As Management.ManagementObjectSearcher = New Management.ManagementObjectSearcher("root/WMI", query)

    '        Dim moc As Management.ManagementObjectCollection = searcher.Get()

    '        Dim moe As Management.ManagementObjectCollection.ManagementObjectEnumerator = moc.GetEnumerator()
    '        moe.MoveNext()

    '        Dim objarr() As Management.ManagementBaseObject = CType(moe.Current.Properties("Ndis80211BSSIList").Value, Management.ManagementBaseObject())
    '        ListBox1.Items.Clear()


    '        For Each obj As Management.ManagementBaseObject In objarr

    '            Dim ssid() As Char = System.Text.Encoding.ASCII.GetChars(CType(obj("Ndis80211Ssid"), Byte()))
    '            ListBox1.Items.Add(New String(ssid))
    '        Next

    '    Catch ex As Exception
    '        Timer1.Enabled = False

    '        MsgBox(ex.Message, MsgBoxStyle.Exclamation)

    '    End Try
    'End Sub
End Class
